﻿using MyClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmDrugScheduleNew : Form
    {
        public FrmDrugScheduleNew()
        {
            InitializeComponent();
        }
        
        MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        ClsDrug clsdrug;
        private void FrmNewDrugSchedule_Load(object sender, EventArgs e)
        {
           
            var q2 = from p in DB.Patients
                     select new { p.姓名, p.病歷號碼, p.病患ID };

            BindingSource bs = new BindingSource();
            bs.DataSource = q2.ToList();
            comboBox1.DataSource = comboBox2.DataSource = bs;
            comboBox1.DisplayMember = "姓名";
            comboBox2.DisplayMember = "病歷號碼";
            comboBox1.ValueMember = "病患ID";

            clsdrug= new DrugFrequency();
            comboBox4.DataSource= clsdrug.GetData();
            comboBox4.DisplayMember = "用藥頻率";
            comboBox4.ValueMember = "轉換天數";

            clsdrug = new TotalDrug();
            comboBox3.DataSource = clsdrug.GetData();
            comboBox3.DisplayMember = "中文名稱";

            clsdrug = new RemindDays();
            comboBox6.DataSource = clsdrug.GetData();
            comboBox6.DisplayMember = "提早天數";
            comboBox6.ValueMember = "提醒天數ID";


            this.comboBox1.SelectedValueChanged += comboBox1_TextChanged;
            comboBox1_TextChanged(sender, e);
           
        }

        void comboBox1_TextChanged(object sender, EventArgs e)
        {            
            var q = from p in DB.Diagnosis
                    where p.RegisterInformation.Patients.病患ID == (int)comboBox1.SelectedValue
                    orderby p.醫囑資料ID descending
                    select new { ID=p.醫囑資料ID };

            comboBox5.DataSource = q.ToList();
            comboBox5.DisplayMember = "ID";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            DB.DrugSchedules.InsertOnSubmit(new MyDB.DrugSchedule
            { 
                醫囑資料ID = int.Parse(comboBox5.Text),
                藥物ID = ((MyDB.DrugOverview)comboBox3.SelectedItem).藥物ID,
                用藥頻率ID = ((MyDB.DrugFrequency)comboBox4.SelectedItem).用藥頻率ID,
                起始日期 = 起始日期DateTimePicker.Value,
                結束日期 = 結束日期DateTimePicker.Value,
                備註 = 備註TextBox.Text,
                結案原因 = 結案原因TextBox.Text,
                結案 = 結案CheckBox.Checked
            });

           
                DB.SubmitChanges();

                MessageBox.Show("新增成功!");
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("新增失敗");
            }

            int sID = DB.DrugSchedules.Select(n => n.用藥排程ID).Max();

            for (DateTime d = 起始日期DateTimePicker.Value.Date; d < 結束日期DateTimePicker.Value.Date; d = d.AddDays((int)comboBox4.SelectedValue))
            {
                MyDB.DrugCalendar ec = new MyDB.DrugCalendar
                {
                    用藥排程ID = sID,
                    狀態ID = 2,
                    提醒天數ID = (int)comboBox6.SelectedValue,                    
                    日期 = d.Date,
                    備註 = 備註TextBox.Text,
                    提醒了沒 = false,                    

                };

                DB.DrugCalendars.InsertOnSubmit(ec);
            }
            try
            {
                DB.SubmitChanges();

                MessageBox.Show("已同步新增行事曆!");
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("行事曆同步失敗");
            }


            ClsRecord.Record(sender, this.Name, (int)comboBox1.SelectedValue);

        }



       
    }
}
